package avicit.bdp.dds.api.controller;

import avicit.bdp.common.dto.CommonVO;
import avicit.bdp.dds.api.dto.OrgDeptUser;
import avicit.bdp.dds.api.service.CommandService;
import avicit.bdp.dds.api.service.DataStatService;
import avicit.bdp.dds.api.service.ProcessDefinitionService;
import avicit.bdp.dds.api.service.ProcessInstanceService;
import avicit.bdp.dds.api.service.ProjectService;
import avicit.bdp.dds.api.service.ProjectTypeService;
import avicit.bdp.dds.dao.entity.Command;
import avicit.bdp.dds.dao.entity.ProcessDefinition;
import avicit.bdp.dds.dao.entity.Project;
import avicit.bdp.dds.dao.entity.ProjectType;
import avicit.platform6.api.system.ConvertColumnClient;
import avicit.platform6.api.system.impl.SystemConstant;
import avicit.platform6.api.sysuser.dto.SysRole;
import avicit.platform6.commons.utils.BusinessUtil;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 数据空间管理REST接口
 */
@RestController
@Api(tags = "数据空间管理REST接口")
@RequestMapping("/api/bdp/dds/project")
public class ProjectRest {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private CommandService commandService;
    @Autowired
    private DataStatService dataStatService;
    @Autowired
    private ProcessDefinitionService processDefinitionService;
    @Autowired
    private ConvertColumnClient convertColumnClient;
    @Autowired
    private ProcessInstanceService processInstanceService;

    @Autowired
    private ProjectTypeService projectTypeService;

    @GetMapping("/getListForProjects")
    @ApiOperation(value = "获取项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "关键字", name = "keyWords", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    public ResponseMsg<QueryRespBean<Project>> getListForProjects(
            @RequestParam(required = false) String keyWords,
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize) {

        ResponseMsg<QueryRespBean<Project>> responseMsg = new ResponseMsg<>();
        QueryRespBean<Project> result = projectService.getListForProjects(keyWords, pageNo, pageSize);
        if (result.getResult() != null) {
            responseMsg.setResponseBody(result);
        } else {
            responseMsg.setResponseBody(null);
        }
        return responseMsg;
    }

    /**
     * 获取所有项目列表
     */
    @GetMapping("/getAllProjectList")
    @ApiOperation(value = "获取所有项目列表")
    public ResponseMsg<List<Project>> getAllProjectList() {
        ResponseMsg<List<Project>> responseMsg = new ResponseMsg<>();
        List<Project> result = projectService.getAllProjectList();
        valueConvert(result);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    @GetMapping("/getListForCommands")
    public ResponseMsg<List<Command>> getListForCommands() {
        ResponseMsg<List<Command>> responseMsg = new ResponseMsg<>();
        List<Command> result = commandService.findCommandList();
        responseMsg.setResponseBody(result);

        return responseMsg;
    }

    @GetMapping("/getListForCommands1")
    public ResponseMsg<Command> getListForCommands1() {
        ResponseMsg<Command> responseMsg = new ResponseMsg<>();
        Command result = commandService.findOneCommand();
        responseMsg.setResponseBody(result);

        return responseMsg;
    }

    @GetMapping("/getListForDefinition")
    public ResponseMsg<List<ProcessDefinition>> getListForDefinition() {
        ResponseMsg<List<ProcessDefinition>> responseMsg = new ResponseMsg<>();
        List<ProcessDefinition> result = processDefinitionService.selectListAll();
        responseMsg.setResponseBody(result);

        return responseMsg;
    }

    /**
     * 查询数据空间列表
     */
    @ApiOperation(value = "查询数据空间列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/getList")
    public ResponseMsg<QueryRespBean<Project>> getList(
            @RequestParam(required = false) String keyWords,
            @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(defaultValue = "10", required = false) Integer pageSize) {

        ResponseMsg<QueryRespBean<Project>> responseMsg = new ResponseMsg<>();
        QueryRespBean<Project> result = projectService.getList(keyWords, pageNo, pageSize);
        valueConvert(result.getResult());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 新增数据空间
     */
    @PostMapping("/insertProject")
    @ApiOperation(value = "新增对象")
    public ResponseMsg<String> insertCategory(@RequestBody Project project) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        project.setOrgIdentity(ThreadContextHelper.getOrgId());
        responseMsg.setResponseBody(projectService.insertProject(project));
        return responseMsg;
    }

    /**
     * 修改数据空间
     */
    @PostMapping("/updateProject")
    @ApiOperation(value = "修改对象")
    public ResponseMsg<String> updateProject(@RequestBody Project project) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        projectService.updateProject(project);
        return responseMsg;
    }

    /**
     * 按主键单条删除
     */
    @ApiOperation(value = "按主键单条删除")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "body", required = true)
    @PostMapping("/deleteProject")
    public ResponseMsg<Integer> deleteProject(@ApiIgnore @RequestBody Map<String, String> queryMap) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String id = queryMap.get("id");
        projectService.deleteProjectById(id);
        return responseMsg;
    }

    /**
     * 数据空间--判断名称是否重复
     */
    @ApiOperation(value = "判断名称是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "body")
    })
    @PostMapping("/isSameName")
    public ResponseMsg<Boolean> isSameName(@ApiIgnore @RequestBody Map<String, Object> qureyMap) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        String name = MapUtils.getString(qureyMap, "name", "");
        String id = MapUtils.getString(qureyMap, "id", "");
        Boolean flag = projectService.isSameName(id, name);
        responseMsg.setResponseBody(flag);
        return responseMsg;
    }

    /**
     * 概览——数据空间
     */
    @GetMapping("/getListFirstProject")
    @ApiOperation(value = "数据空间概览")
    public ResponseMsg<List<Project>> getListFirstProject() {
        ResponseMsg<List<Project>> responseMsg = new ResponseMsg<>();
        List<Project> result = projectService.getListFirstProject();
        valueConvert(result);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 通过平台API将字段值转换为名称，包括通用选择组件、通用代码
     */
    private void valueConvert(List<Project> projectList) {
        // 循环组装请求数据
        Map<String, Set<String>> convertFormData = new HashMap<>();
        for (Project project : projectList) {
            BusinessUtil.createConvertSet(convertFormData, SystemConstant.USER, project.getCreatedBy());
        }
        if (convertFormData.size() > 0) {
            // 获取请求结果
            Map<String, Map<String, String>> convertResultData =
                    convertColumnClient.replace(convertFormData);
            // 循环设置Alias或Name的值
            for (Project project : projectList) {
                project.setUsername(
                        BusinessUtil.convertFormat(
                                convertResultData, SystemConstant.USER, project.getCreatedBy()));
            }
        }
    }

    /**
     * 公共模型导入
     */
    @ApiOperation(value = "公共模型导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelIds", value = "模型id集合", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "projectId", value = "数据空间id", dataType = "String", paramType = "body", required = true),
            @ApiImplicitParam(name = "typeId", value = "分类id", dataType = "String", paramType = "body", required = true)
    })
    @PostMapping("/importCommonModel")
    public ResponseMsg<Integer> importCommonModel(@ApiIgnore @RequestBody Map<String, String> queryMap) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String modelIds = MapUtils.getString(queryMap, "modelIds", "");
        String projectId = MapUtils.getString(queryMap, "projectId", "");
        String typeId = MapUtils.getString(queryMap, "typeId", "");
        projectService.importCommonModel(modelIds, projectId, typeId);
        return responseMsg;
    }

    /**
     * 获取人员信息
     */
    @ApiOperation(value = "获取人员信息")
    @ApiImplicitParam(name = "projectId", value = "数据空间id", dataType = "String")
    @GetMapping("/getUserList")
    public ResponseMsg<List<OrgDeptUser>> getUserList(@RequestParam(required = false) String projectId) {
        ResponseMsg<List<OrgDeptUser>> responseMsg = new ResponseMsg<>();
        List<OrgDeptUser> orgDeptUserList = projectService.getUserList(projectId);
        responseMsg.setResponseBody(orgDeptUserList);
        return responseMsg;
    }

    /**
     * 获取角色信息
     */
    @GetMapping("/getRoleList")
    @ApiOperation(value = "获取角色信息")
    public ResponseMsg<List<SysRole>> getRoleList() {
        ResponseMsg<List<SysRole>> responseMsg = new ResponseMsg<>();
        List<SysRole> roleList = projectService.getRoleList();
        responseMsg.setResponseBody(roleList);
        return responseMsg;
    }

    /**
     * 获取概览折线图
     *
     * @param projectId 项目空间ID
     */
    @ApiOperation(value = "获取概览折线图")
    @ApiImplicitParam(value = "数据空间ID", name = "projectId", dataType = "String")
    @GetMapping(value = "/getProcessLine")
    public ResponseMsg<Map<String, Object>> getProcessLine(
            @RequestParam(value = "projectId", required = false) String projectId) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dataStatService.getProcessLine(projectId));
        return responseMsg;
    }

    /**
     * 获取概览饼状图
     *
     * @param projectId 项目空间ID
     */
    @ApiOperation(value = "获取概览饼状图")
    @ApiImplicitParam(value = "数据空间ID", name = "projectId", dataType = "String")
    @GetMapping(value = "/getProcessPie")
    public ResponseMsg<List<Map<String, Object>>> getProcessPie(
            @RequestParam(value = "projectId", required = false) String projectId) {
        ResponseMsg<List<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(processInstanceService.getProcessPie(projectId));
        return responseMsg;
    }

    /**
     * 获取重点关注
     *
     * @param projectId 项目空间ID
     */
    @ApiOperation(value = "重点关注")
    @ApiImplicitParam(value = "数据空间ID", name = "projectId", dataType = "String")
    @GetMapping(value = "/getFocus")
    public ResponseMsg<Map<String, Object>> getFocus(
            @RequestParam(value = "projectId", required = false) String projectId) {

        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(projectService.getFocus(projectId));
        return responseMsg;
    }

    @GetMapping(value = "/getDefaultProject")
    @ApiOperation(value = "获取用户默认空间")
    public ResponseMsg<Project> getDefaultProject() {

        ResponseMsg<Project> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(projectService.getDefaultProject());
        return responseMsg;
    }

  @GetMapping(value = "/getPhmDefaultProject")
  @ApiOperation(value = "获取用户默认空间,支持PHM")
  public ResponseMsg<Project> getPhmDefaultProject(String projectType, String phmProjectId, String phmProjectName) {

    ResponseMsg<Project> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(projectService.getPhmDefaultProject(projectType, phmProjectId, phmProjectName));
    return responseMsg;
  }

    /**
     * 设置用户默认空间
     */
    @ApiOperation(value = "设置用户默认空间")
    @PostMapping("/setDefaultProject")
    public ResponseMsg<String> setDefaultProject(@RequestBody CommonVO commonVO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String projectId = commonVO.getId();
        projectService.setDefaultProject(projectId);
        return responseMsg;
    }

    /**
     * 获取空间下流程类型列表
     */
    @ApiOperation(value = "获取空间下流程类型列表")
    @ApiImplicitParam(value = "数据空间类型", name = "projectType", dataType = "String")
    @GetMapping(value = "/getProcessTypeList")
    public ResponseMsg<List<ProjectType>> getProcessTypeList(
            @RequestParam(value = "projectType", required = false) String projectType) {
        ResponseMsg<List<ProjectType>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(projectTypeService.getProcessTypeList(projectType));
        return responseMsg;
    }

    /**
     * 判断当前用户是否为空间创建人
     */
    @ApiOperation(value = "判断当前用户是否为空间创建人")
    @ApiImplicitParam(value = "数据空间id", name = "projectId", dataType = "String")
    @GetMapping(value = "/projectCreateAble")
    public ResponseMsg<Boolean> projectCreateAble(
            @RequestParam(value = "projectId") String projectId) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(projectService.projectCreateAble(projectId));
        return responseMsg;
    }

    @GetMapping(value = "/getProjectById")
    @ApiOperation(value = "获取空间信息")
    @ApiImplicitParam(value = "数据空间id", name = "id", dataType = "String")
    public ResponseMsg<Project> getProjectById(@RequestParam(value = "id") String id) {

        ResponseMsg<Project> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(projectService.selectByPrimaryKey(id));
        return responseMsg;
    }
}
